'use strict';

export default class extends think.service.base {
    /**
     * init
     * @return {}         []
     */
    init() {
        this.Work = this.model('weal_active');
        let redisService = think.service('redisservice', 'redisservice');
        this.redisService = new redisService();
    }
    async addWork(obj) {
        let self = this;
        try {

            await self.Work.startTrans();
            obj['active'] = 0;
            let uId = await self.Work.addWork(obj)
            await self.Work.commit();
            return uId;
        } catch (e) {
            await self.Work.rollback();
            return 101;
        }
    }

    async getWork(post) {
        console.log('getWork')
        let self = this;
        let type = post
        let allParams = {};
        switch (post.orderBy) {
            case '1':
                allParams = {
                    order: {
                        id: 'DESC'
                    }
                }
                break;
            case '2':
                allParams = {
                    order: {
                        active: 'DESC'
                    }
                }
                break;
            default:
                allParams = {
                    order: {
                        active: 'DESC'
                    }
                }
                break;
        }
        var Work;
        if (post.work_id) {
            allParams['where'] = {
                id: post.work_id
            }
        }
        Work = await this.Work.getWork(allParams);
        return Work;
    }


    async updataAction() {

    }

    async pollAdd(allParams) {
        let self = this;
        console.log(123312)
        console.log(allParams)
        if (allParams.openid) {
            let user = this.model('user', 'user')
            let _user = await user.getUser({
                openid: allParams.openid
            });
            if (think.isEmpty(_user)) {
                return 111;
            }
        } else {
            console.log('没有openid')
            return 111;
        }
        var count;
        console.log('获取count  openid=' + allParams.openid);
        await this.redisService.get('count' + allParams.openid).then((data) => {
            count = data;
            console.log('count=' + count)
        });
        // await this.redisService.setClearTime('count' + allParams.openid,1);
        if (think.isEmpty(count)) {
            console.log('新建count  openid=' + allParams.openid);
            await this.redisService.add('count' + allParams.openid);
            console.log('新建count  openid=' + allParams.openid + '成功')
            await this.redisService.updata('count' + allParams.openid);
            console.log('更新count  openid=' + allParams.openid + '成功')
            console.log('添加定时清除');
            let time = await this.getIndexDayTime();
            await this.redisService.setClearTime('count' + allParams.openid, Math.ceil(time));
            console.log('添加定时清除成功' + time + "秒之后清除");
        } else {
            if (count > 9) {
                return 112;
            }
            console.log('Work' + Work)
            await this.redisService.updata('count' + allParams.openid);
            console.log('该count  openid=' + allParams.openid + '已经存在更新成功')
        }
        let obj;
        if (allParams.work_id) {
            obj = {
                where: {
                    id: allParams.work_id
                }
            }

        } else {
            return 111;
        }
        let Work = await self.Work.pollAdd(obj);
        console.log('toupiao' + Work)
        return Work

    }

    async getIndexDayTime() {
        let D = new Date()
        let date = new Date(D.getFullYear() + "-" + (D.getMonth() + 1) + "-" + D.getDate() + "-23:59:59");
        let timeStamp = date.getTime();
        let indexTimeStamp = (new Date()).getTime();
        let clearTime = ((timeStamp - indexTimeStamp) / 1000);
        return clearTime;
    }
}
